package com.atguigu.yygh.hosp.controller.admin;

import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.atguigu.yygh.common.utils.MD5;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * <p>
 * 医院设置表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2023-05-11
 */
//医院设置接口
@RestController
@Api(tags = "医院设置信息")
@RequestMapping("/admin/hosp/hospitalSet")
@Slf4j
//@CrossOrigin//这个注释是局部解决（跨域问题的）====注意不是全局的，全局的后面会将
public class HospitalSetController {

    @Resource
    private HospitalSetService hospitalSetService;

    /**
     * @description:锁定与解锁
     * @author :又菜又爱玩的→速趴塞亚锦←
     * @create :2023/5/12
     * @param  :1.8
     */
    @PutMapping("/status/{id}/{status}")
    public R updateStatus(@PathVariable Long id,@PathVariable Integer status){
        log.info("current thread is 当前线程为 "+Thread.currentThread().getId()+",params:id="+id);
        //int i=10/0;
        HospitalSet hospitalSet=new HospitalSet();
        hospitalSet.setId(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        //HospitalSet byId = hospitalSetService.getById(id); //乐观锁
        log.info("result 结果 "+Thread.currentThread().getId()+hospitalSet.toString());
        return R.ok();
    }

    /**
     * @description:批量删除
     * @author :又菜又爱玩的→速趴塞亚锦←
     * @create :2023/5/12
     * @param  :1.8
     */
    @DeleteMapping("/delete")
    public R batchDelete(@RequestBody List<Integer> ids){
        hospitalSetService.removeByIds(ids);
        return R.ok();
    }

    /**
     * @description:修改之回显数据
     * @author :又菜又爱玩的→速趴塞亚锦←
     * @create :2023/5/12
     * @param  :1.8
     */
    @GetMapping("/detail/{id}")
    public R detail(@PathVariable Integer id){
        return R.ok().data("item",hospitalSetService.getById(id));
    }

    /**
     * @description:修改之修改数据
     * @author :又菜又爱玩的→速趴塞亚锦←
     * @create :2023/5/12
     * @param  :1.8
     */
    @PutMapping("/update")
    public R update(@RequestBody HospitalSet hospitalSet){
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }

    /**
     * @description:新增接口_使用到了MD5加密
     * @author :又菜又爱玩的→速趴塞亚锦←
     * @create :2023/5/12
     * @param  :1.8
     */
    @ApiOperation(value = "新增接口")
    @PostMapping("/save")
    public R save(@RequestBody HospitalSet hospitalSet){
        //设置状态 1 使用 0 不能使用*********
        hospitalSet.setStatus(1);
        //当前时间戳+随机数+MD5加密
        Random random=new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        hospitalSetService.save(hospitalSet);

        return R.ok();
    }

    /**
     * @description:带查询条件的分页
     * @author :又菜又爱玩的→速趴塞亚锦←
     * @create :2023/5/12
     * @param  :1.8
     */
    @ApiOperation(value = "带查询条件的分页")
    @PostMapping(value = "/page/{pageNum}/{size}")
    public R getPageInfo(@ApiParam(name = "pageNum",value = "当前页")@PathVariable Integer pageNum,
                         @ApiParam(name = "size",value = "每页显示多少条") @PathVariable Integer size,
                         @RequestBody HospitalSetQueryVo hospitalSetQueryVo){

        Page<HospitalSet> page=new Page<HospitalSet>(pageNum,size);

        QueryWrapper<HospitalSet> queryWrapper=new QueryWrapper<HospitalSet>();
        if(!StringUtils.isEmpty(hospitalSetQueryVo.getHosname())){
            queryWrapper.like("hosname",hospitalSetQueryVo.getHosname());
        }
        if(!StringUtils.isEmpty(hospitalSetQueryVo.getHoscode())){
            queryWrapper.eq("hoscode",hospitalSetQueryVo.getHoscode());
        }

        hospitalSetService.page(page, queryWrapper);
        return R.ok().data("total",page.getTotal()).data("rows",page.getRecords());

    }

    //访问地址是：localhost:8201/admin/hosp/hospitalSet/findAll
    //谷歌不加：htth://...
    //查询所有医院设置
    @GetMapping("findAll")
    public R findAll(){
        List<HospitalSet> list = hospitalSetService.list();
        return R.ok().data("items",list);
    }
    //访问地址是：htth://localhost:8201/admin/hosp/hospitalSet/deleteById
    //谷歌不加：htth://...
    //根据医院设置id删除医院设置信息
    @ApiOperation(value = "根据医院设置id删除医院设置信息")
    @DeleteMapping(value = "/deleteById/{id}")
    //@ApiParam(name = "id",value = "医院设置id",required = true) @RequestParam(value = "id") Integer
    //用/deleteById/{id}这个的话就不用上面的这个方法啦===用这个@PathVariable
    public R deleteById(@PathVariable Integer id){
        hospitalSetService.removeById(id);
        return R.ok();
    }
    //string: prefix +string+suffix.html:PC
    //项目 json格式：PC+物手机+联网
    /*=============================================
          @Api(tags=""):标记在接口类上
          @ApiOperation(value=""):标记在方法上
          @ApiParam(value=""):标记在参数上
          @ApiModel(description=")：对POJO类做说明
          @ApiModelProperty(value=")：对POJO类属性做说明
      ============================================*/
}